home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c
- Subject: Re: Basic Question on SWITCH
- Date: 29 Jan 1996 08:17:49 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4eikud$e7b@solutions.solon.com>
- References: <4e4cu4$95f@vixen.cso.uiuc.edu>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4e4cu4$95f@vixen.cso.uiuc.edu>, HOTARD <jhotard> wrote:
- >I am just learning how to program in C, and I had a question about switch.
- >I am writing a program that looks at a character and then determines if it is a
- >letter or number. This program must use the switch command , can I place a
- >range on the case command somehow??
-
- >ie: Switch (var)
- > case 0-9:
-
- >or case (isdigit):
-
- >would anything like this work???
-
- No.
-
- One can only assume from "This program must use the switch command" that
- this is homework; especially because switch is not a "command".
-
- I dare you to try this:
- main() {
- int c, x, printf();
- c = getchar();
- switch (x = ((c = (unsigned char) c), /* try to ensure a legal char. */
- ((!!isdigit(c)) << 1) | ((!!isalpha(c)) << 0)))
- default:
- printf(
- (x == 0 ? "'\\x%x' is neither a letter nor a number.\n" :
- (x == 1 ? "'%c' is a letter.\n" : (x == 2 ? "'%c' is a number.\n" :
- (x == 3 ? "'%c' is both a letter and a number.\n" :
- "'\\x%x' is odd.\n"))), c);
- return 0;
- }
-
- It uses a switch, and should correctly identify the first character of
- standard input.
-
- I leave making it into a loop as an exercise for the reader.
-
- As far as I can tell, this is legal and strictly conforming ANSI.
-
- -s
- p.s.: Which is not to say it's good code.
- --
- Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
- C/Unix wizard -- C/Unix questions? Send mail for help. No, really!
- Using trn? Weird new newsgroup problem? I know the fix! Email me!
- The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
-